import pygame
import buttons
from pygame.sprite import *
from pygame.locals import *
from players import *

class Cursor(pygame.sprite.Sprite):
    cursor_group = pygame.sprite.Group()
    cursor = None

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.cursor= pygame.sprite.Sprite( self.cursor_group )
        self.cursor.image = pygame.image.load('graphics/cursor.png')
        self.cursor.rect = self.cursor.image.get_rect()
        self.cursor_group.add(self.cursor)

    def get_sprite(self):
        return self.cursor

    def update(self):
        pos = pygame.mouse.get_pos()
        self.cursor.rect.x = pos[0]
        self.cursor.rect.y = pos[1]

    def draw(self,screen):
        
        self.cursor_group.draw(screen)

class Game():
    screen = None
    background_image = pygame.image.load('graphics/background_1.png')#.convert()
    cursor_image = None
    buttons = None
    cursor = Cursor()
    enemy_castle = None
    my_castle = None
    enemy_troops = Group()
    power_bars = Powers()
    my_troops = Group()

    def __init__(self, screen):
        self.screen = screen
        self.buttons = buttons.Buttons()

        self.my_castle = Troop("my_evolution1_castle")
        self.my_troops.add(self.my_castle)
        self.power_bars.add(self.my_castle.power)


        self.enemy_castle = Troop("enemy_evolution1_castle")
        self.enemy_troops.add(self.enemy_castle)
        self.power_bars.add(self.enemy_castle.power)


    def is_end(self):
        return False

    def handle_event(self,event):
        if event.type == MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
            for i in  pygame.sprite.spritecollide(self.cursor.get_sprite(), self.buttons.unit_buttons, 0):
                player = Player(i.get_key())
                self.my_troops.add( player )
                self.power_bars.add( player.power )

    def end_game(self):
        pass


    def handle_collisions(self):
        for my in self.my_troops:
            if isinstance(my,Player):
                my.action = PLAYER_MOVE
        for enemy in self.enemy_troops:
            if isinstance(enemy, Player):
                enemy.action = PLAYER_MOVE
        ## colliding my_troops with enemy_troops
        collisions = pygame.sprite.groupcollide(self.my_troops, self.enemy_troops, 0,0)
        for i in collisions:
            if isinstance(i, Player):
                i.attack(collisions[i][0])
            for j in collisions[i]:
                if isinstance(j, Player):
                    j.wait()
            if isinstance( collisions[i][0], Player ):
                collisions[i][0].attach()
        ## colliding my_troops with my_troops. making some wait
        my_collisions = pygame.sprite.groupcollide(self.my_troops, self.my_troops,0,0)
        for i in my_collisions:
            if isinstance(i, Player) and len(my_collisions[i]) > 1:
                for j in my_collisions[i]:
                    if isinstance(j, Player):
                        if j != i:
                            if not i.is_can_move_on_self_collide(j):
                                j.action = PLAYER_WAIT
                            else:
                                i.action = PLAYER_WAIT


    def draw_game(self):
        self.handle_collisions()
        self.screen.blit( self.background_image, (0,0))
        self.buttons.draw(self.screen)
        self.my_troops.update()
        self.my_troops.draw(self.screen)
        self.enemy_troops.update()
        self.enemy_troops.draw(self.screen)
        self.power_bars.draw(self.screen)
        self.cursor.update()
        self.cursor.draw( self.screen )

        pygame.display.update()
        pygame.display.flip()

